﻿using System;
using System.Collections.Generic;
using System.Text;
using SysTmr = System.Timers;
using SysThd = System.Threading;
using System.Net;
using System.Net.Mail;

namespace ipnotifd
{
    class Program
    {
        static string _currentIp= string.Empty;
        static string IP_ERROR  = "xxx.xxx.xxx.xxx";

        static SysTmr.Timer _timer  = null;

        static void Main (string[] args)
        {
            onTimerElapsed(null, null); // inmediate first time

            _timer = new SysTmr.Timer(60*1000);
            _timer.Elapsed += new SysTmr.ElapsedEventHandler(onTimerElapsed);
            _timer.Start();

            Console.ReadLine();
        }

        static void onTimerElapsed(object sender, SysTmr.ElapsedEventArgs e)
        {
            //string newIp = getMyIpv4 (
            //    new WebProxy            ("sm-isa-01", 8080),
            //    new NetworkCredential   ("avarela", "gorgonzola")
            //);

            string newIp = getMyIpv4();

            Console.WriteLine(newIp);

            if (newIp == IP_ERROR)
            {
                Console.WriteLine("error getting ip");
            }
            else
            {
                if (_currentIp != newIp)
                {
                    notifyIpChange(_currentIp, newIp);
                    _currentIp = newIp;
                }
            }
        }

        static void notifyIpChange(string currentIp, string newIp)
        {
            sendMail("hav29a@gmail.com", "ipnotifd::change", newIp);
            sendMail("avarela@rosariobus.com.ar", "ipnotifd::change", newIp);
        }

        static void sendMail(string to, string subject, string body)
        {
            SmtpClient client = new SmtpClient();
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            //client.Port = 465;
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(
                "hav29a@gmail.com", "oculus reparo"
            );
            MailMessage mail = new MailMessage(
                "hav29a@gmail.com", to
            );
            mail.Subject= subject;
            mail.Body   = body;
            client.Send (mail);
        }

        static string getMyIpv4()
        {
            return getMyIpv4(null, null);
        }

        static string getMyIpv4(WebProxy proxy, NetworkCredential creds)
        {
            try
            {
                WebClient cli = new WebClient();
                if (proxy != null)
                    cli.Proxy = proxy;
                
                if ((proxy != null) && (creds != null)) 
                    cli.Proxy.Credentials = creds;
                
                string ipv4 = cli.DownloadString(
                    "http://ipv4.icanhazip.com"
                );

                return ipv4.Trim();
            }
            catch (Exception exx)
            {
                Console.WriteLine(exx.Message);
                return IP_ERROR;
            }
        }
    }
}
